package com.demo.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.demo.dao.UserDao;
import com.demo.entity.User;

@Component("userDao")
public class UserDaoImpl implements UserDao {

	private SessionFactory sessionFactory ;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	/**
	 * load method
	 */
	@Override
	public List<User> load(String name,String password){
		
		Session session =  sessionFactory.openSession();
		session.beginTransaction();
		/*hibernate 官方推荐的写法*/
		Query query = session.createQuery("from User where name=:name_ and password=:password_"); 
		/*给hql赋值*/
		query.setString("name_", name);
		query.setString("password_", password);
		@SuppressWarnings("unchecked")
		List<User> list = query.list();
		
		session.close();
		return list;
	}

}
